package com.itheima.reggie.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 这个时redis存储的时候不会出现斜杠的配置文件
 */
@SuppressWarnings("all")
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    /**
     * 自己配置redisTemplate
     *
     * @return
     */
    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory factory) {

        //1. 创建redisTemplate
        RedisTemplate redisTemplate = new RedisTemplate();

        //2. 设置key的序列化器

        //针对字符串String
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        //针对Hash
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());

        //3. 设置连接工厂
        redisTemplate.setConnectionFactory(factory);

        return redisTemplate;
    }
}
